//
//  TimeSettingView.m
//  TaiHao
//
//  Created by rzm on 16/6/2.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "TimeSettingView.h"
//#import "TimeTableViewCell.h"
#import "THTimeCell.h"
#import "THSettingTimeModel.h"

@interface TimeSettingView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *modelArr;
    NSArray *speedArr;
}
//@property(nonatomic,strong) NSIndexPath *indexPath;

@end

@implementation TimeSettingView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style]) {

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveTimeNotification:) name:@"THNotificationCenter_settingTime" object:nil];
        self.delegate = self;
        self.dataSource = self;

        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    return self;
    
}


- (void)setTimeStyle:(THSettingMyDeviceViewControllerFrom)timeStyle
{
    _timeStyle = timeStyle;

    [self getDataSource:_timeStyle];
    
   
}
- (void)getDataSource:(THSettingMyDeviceViewControllerFrom)timeStyle
{
    [self reloadData];

    modelArr = [NSMutableArray array];
    speedArr = [NSMutableArray array];
    NSArray *arr1 = @[@"23:32",@"15:09",@"09:23",@"20:56"];
    NSArray *arr2 = @[@"11:44",@"01:09",@"23:23",@"21:45"];
    NSArray *arr3 = @[@"14:32",@"08:09",@"01:23",@"13:56"];
    NSArray *arr4 = @[@"22:32",@"11:11",@"18:23",@"17:24"];
    
    NSMutableArray *dateArr = [NSMutableArray arrayWithObjects:arr1,arr2,arr3,arr4,arr2,arr4,arr1, nil];
    NSArray *speedArr1 = @[@"2 X",@"4 X",@"1 X",@"7 X"];
    NSArray *speedArr2 = @[@"3 X",@"2 X",@"6 X",@"8 X"];
    NSArray *speedArr3 = @[@"5 X",@"8 X",@"3 X",@"7 X"];
    NSArray *speedArr4 = @[@"3 X",@"6 X",@"3 X",@"2 X"];
    
    NSArray *speedArr_1 = @[@"高效",@"静音",@"关",@"中效"];
    NSArray *speedArr_2 = @[@"静音",@"静音",@"关",@"静音"];
    NSArray *speedArr_3 = @[@"中效",@"静音",@"高效",@"关"];
    if (timeStyle == pushFromCabinet) {
       speedArr = [NSMutableArray arrayWithObjects:speedArr1,speedArr2,speedArr3,speedArr4,speedArr2,speedArr4,speedArr1, nil];

    }else
    {
        speedArr = [NSMutableArray arrayWithObjects:speedArr_1,speedArr_3,speedArr_2,speedArr_1,speedArr_3,speedArr_1,speedArr_3, nil];

    }

    
    for (int i = 0 ; i < 7; i ++) {
        THSettingTimeModel *model = [THSettingTimeModel alloc];
        [modelArr addObject:model];
        NSArray *date = dateArr[i];
        NSArray *speed = speedArr[i];
        model.timeText_morning = date[0];
        model.timeText_noon = date[1];
        model.timeText_after = date[2];
        model.timeText_night = date[3];
        
        model.speedText_morning = speed[0];
        model.speedText_noon = speed[1];
        model.speedText_after = speed[2];
        model.speedText_night = speed[3];
    }
    
    

}

#pragma mark UITableViewCellDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    THTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCell"];
    if (cell == nil) {
        cell = [[THTimeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"timeCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tag = indexPath.row + 10;
    NSArray *weekArr = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
    cell.weeklab.text = weekArr[indexPath.row];
    
    if (modelArr[indexPath.row] == nil) {
//        [self reloadData];
    }else
    {
        cell.model = modelArr[indexPath.row];

    }
    return cell;
    
}


#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return TH6HScale(206);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    THTimeCell *cell = [self cellForRowAtIndexPath:indexPath];
    cell.subView.backView.backgroundColor = [UIColor redColor];
    
    if (self.timeDelegate && [self.timeDelegate respondsToSelector:@selector(timeSettingSelectedCellSub: andSubData:)]) {
        [self.timeDelegate timeSettingSelectedCellSub:indexPath.row andSubData:modelArr[indexPath.row]];
        THSettingTimeModel *mode = modelArr[indexPath.row];
        
        NSLog( @"mode.timeText_morning==%@",mode.timeText_morning);
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    THTimeCell *cell = [self cellForRowAtIndexPath:indexPath];
    cell.subView.backView.backgroundColor = [UIColor clearColor];
}

- (void)reciveTimeNotification:(NSNotification *)notification
{
    THSettingDetailTimeModel *model = notification.object;
    NSArray *btnArr = model.selectedBtnArr;
    
    for (int i = 0 ; i < btnArr.count; i++) {
        UIButton *btn = btnArr[i];
        NSInteger indexTag = btn.tag;
        if (indexTag == 10000) {
            indexTag = btn.tag - 10000 + 6;
        }else if (btn.tag == 10007)
        {
           indexTag = btn.tag - 10000;
        }else
        {
            indexTag = btn.tag - 10000 - 1;
        }
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexTag inSection:0];        
        THTimeCell *cell = [self cellForRowAtIndexPath:indexPath];
        NSInteger type = model.selectedCellType;
        THTimeTableIViewCellSub *sub = [cell viewWithTag:model.selectedCellType + 100];
        THSettingTimeModel *timeModel = cell.model;
        
        if (type == 0) {
            timeModel.speedText_morning = model.speedStr;
             timeModel.timeText_morning = model.timeStr;
        }else if (type == 1)
        {
            timeModel.speedText_noon = model.speedStr;
            timeModel.timeText_noon = model.timeStr;
        }else if (type == 2)
        {
            timeModel.speedText_after = model.speedStr;
            timeModel.timeText_after = model.timeStr;
        }else{
            timeModel.speedText_night = model.speedStr;
            timeModel.timeText_night = model.timeStr;
        }
        cell.model = timeModel;
    }
}


@end
